<?php

/**
 * Description of DALCountry
 *
 * @author aethv
 */
class DALCountry extends DALBase {

    public $dalLeague;
    const SEPERATE = 'xx00xx';

    public function __construct() {
        parent::__construct();
        $this->_tbl = 'country';
        $this->_key = 'id';
        $this->dalLeague = new DALLeague();
    }

    function parseAndPersistCountry($obj) {
        if (is_array($obj)) {
            for ($index = 0; $index < count($obj); $index++) {
                $country = $obj[$index];
                $c = new Country($country->id, $country->name, $country->url, 1);
                $insertedId = $this->save($c);
                if ($insertedId > 0) {
                    $this->parseAndPersistLeague($country);
                }
            }
        }
        return 1;
    }

    function updateCountryConfig($obj) {
        $countryRunning = array();
        $countryStop = array();
        for ($i = 0; $i < count($obj); $i++) {
            $row = $obj[$i];
            if ($row['status'] == 0) {
                $countryStop[] = $row['id'];
            } else {
                $countryRunning[] = $row['id'];
            }
        }
        if (count($countryRunning) > 0) {
            $listId = implode(',', $countryRunning);
            $sql = "UPDATE country SET STATUS = 1 WHERE id IN ($listId)";
            $this->executeQuery($sql);
        }
        if (count($countryStop) > 0) {
            $listId = implode(',', $countryStop);
            $sql = "UPDATE country SET STATUS = 0 WHERE id IN ($listId)";
            $this->executeQuery($sql);
        }
    }

    function parseAndPersistLeague($country) {
        $leagues = $country->leagues;
        if (is_array($leagues)) {
            for ($index1 = 0; $index1 < count($leagues); $index1++) {
                $league = $leagues[$index1];
                $l = new League($league->id, $league->name, $country->id);

                $this->dalLeague->save($l);
            }
        }
    }

    public function save($data) {
        $query = "";

        $id = $data->id;
        $tblID = $this->getByID($id);
        if ($tblID) {
            $query = "UPDATE $this->_tbl SET ";
            $query .= " `modify_timestamp`=now()";
            $query .= " WHERE `id`=$id";
        } else {
            $query = "INSERT INTO $this->_tbl (`id`, `name`, `url`, `status`) VALUES (";
            $query .= $id;
            $query .= " ,'$data->name'";
            $query .= " ,'$data->url'";
            $query .= " ,0";
            $query .= " );";
        }
//        echo $query;
        $result = $this->executeQuery($query);
        if ($result) {
            if ($tblID) {
                return $tblID;
            }
            return mysql_insert_id();
        }
        return -1;
    }

    public function getCountriesClosing(){
        $query = "select * from $this->_tbl "
                . "where modify_timestamp > DATE_SUB(now(), INTERVAL 12 HOUR) "
                . "ORDER BY modify_timestamp desc";
        $data = array();
        $result = $this->executeQuery($query);
        if ($result) {
            //  Scan through the resource
            while ($row = mysql_fetch_object($result)) {
                //  put row object into the array
                $data[] = $row;
            }
        }
        //
        return $data;
    }
}
